import java.util.ArrayList;

import ij.ImagePlus;
import ij.process.ImageProcessor;

public class band {

	// gridX[pie1] = xcord of center of mass of pie1 ; gridY[pie1] = ycord of
	// center of mass of pie1 ; gridDist[pie1] = Distance of point from center
	// of mass;
	// " for pie2
	// " for pie3
	// "
	// "
	// " for pie NUM_GRID_CELL

	//static float[] gridDist = new float[Analysis_.NUM_GRID_CELL];
	//static float[] gridX = new float[Analysis_.NUM_GRID_CELL];
	//static float[] gridY = new float[Analysis_.NUM_GRID_CELL];
	//static int[] gridCount = new int[Analysis_.NUM_GRID_CELL];
	int c, r, g, b;

	
	static int numVerts = 0; 
	static vertex[] ve = null;
	static edge[] ed; 
	
	static ArrayList<vertex> ve1 = new ArrayList<vertex>();
	
	public void find_vertex(ImagePlus imp, ImagePlus mask_Channel1, ImagePlus mask_Channel2) {

		 float[] gridDist = new float[Analysis_.NUM_GRID_CELL];
		 float[] gridX = new float[Analysis_.NUM_GRID_CELL];
		 float[] gridY = new float[Analysis_.NUM_GRID_CELL];
		 int[] gridCount = new int[Analysis_.NUM_GRID_CELL];
		
		System.out.println("xMass in band: "+center_mass.xMass);
		System.out.println("yMass in band: "+center_mass.yMass);
		System.out.println("Num grid cell band"+Analysis_.NUM_GRID_CELL);
		
		
		ImageProcessor ip = imp.getProcessor();
		ImageProcessor mask_ip_1 = mask_Channel1.getProcessor();
		ImageProcessor mask_ip_2 = mask_Channel2.getProcessor();
		
		//imp.show();

		for (int y = 0; y < imp.getHeight(); y++) {
			for (int x = 0; x < imp.getWidth(); x++) {

				float fx = x - center_mass.xMass;
				float fy = y - center_mass.yMass;

				double hyp = Math.sqrt(fx * fx + fy * fy);
				double angle = Math.acos(fx / hyp);
				if (fy < 0)
					angle = 2 * Math.PI - angle;

				c = ip.getPixel(x, y);
				r = (c & 0xff0000) >> 16;
				g = (c & 0xff00) >> 8;
				b = c & 0xff;

				
				// *************************************  To be checked mass pixel has value 0************************
				// color color is yellow and pixel belongs to both actin mask
				// and myosin mask
				if (r == 255 && g == 255 && (mask_ip_1.getPixel(x, y) == 255) && (mask_ip_2.getPixel(x, y) == 255) ) {
					ip.putPixel(x, y, c);

					//System.out.println("Inside for loop");
					// hack: dist of pixel from center of mass should be > 100
					if (hyp >= 100) {
						int gridCell = (int) (Analysis_.NUM_GRID_CELL * angle / (2 * Math.PI));
						if (gridCell < 0)
							gridCell = 0;
						if (gridCell > Analysis_.NUM_GRID_CELL - 1)
							gridCell = Analysis_.NUM_GRID_CELL - 1;
						gridDist[gridCell] += hyp;
						gridX[gridCell] += x;
						gridY[gridCell] += y;
						gridCount[gridCell]++;
					}

				} else {
					//ip.putPixel(x, y, 999999999);
				}
			}
		}

		for (int i = 0; i < Analysis_.NUM_GRID_CELL; i++) {
			gridDist[i] /= gridCount[i];
			gridX[i] /= gridCount[i];
			gridY[i] /= gridCount[i];
		}

		// /
		ve =  new vertex[Analysis_.NUM_GRID_CELL];

		for (int i = 0; i < Analysis_.NUM_GRID_CELL; i++) {
			ve[i] = new vertex();
		}
		numVerts = 0;

		// number of selected vertex
		for (int i = 0; i < Analysis_.NUM_GRID_CELL; i++) {
			if (gridCount[i] > 5) {
				ve[numVerts].x = (int) gridX[i];
				ve[numVerts].y = (int) gridY[i];
				ve[numVerts].gridid = i;
				numVerts++;
				//System.out.println("Incrementing num verts"+i);
			}

		}

		System.out.println("Number of selected vertices: " + numVerts);

		if(numVerts > 2){
		ed = new edge[numVerts];

		for (int i = 0; i < numVerts; i++) {
			ed[i] = new edge();
		}

		for (int i = 0; i < numVerts - 1; i++) {

			if (ve[i].x != 0 && ve[i].y != 0 && ve[i + 1].x != 0
					&& ve[i + 1].y != 0) {

				ed[i].x1 = ve[i].x;
				ed[i].x2 = ve[i + 1].x;
				ed[i].y1 = ve[i].y;
				ed[i].y2 = ve[i + 1].y;
			}
		}

		ed[numVerts - 1].x1 = ve[numVerts - 1].x;
		ed[numVerts - 1].y1 = ve[numVerts - 1].y;
		ed[numVerts - 1].x2 = ve[0].x;
    	ed[numVerts - 1].y2 = ve[0].y;
 
    	
		//for (int i = 0; i < numVerts; i++) {

			//ip.drawLine(center_mass.xMass, center_mass.yMass, (int) ve[i].x, (int) ve[i].y);
		//}
    	}
		imp.updateAndDraw();

	}
	
	public void user_defined_vertices( )
	{
		//ImageProcessor ip = imp.getProcessor();
		
		
		ed = new edge[Analysis_.numVertex_User_Defined];

		for (int i = 0; i < Analysis_.numVertex_User_Defined; i++) {
			ed[i] = new edge();
		}

		for (int i = 0; i < Analysis_.numVertex_User_Defined - 1; i++) {

			if (ve[i].x != 0 && ve[i].y != 0 && ve[i + 1].x != 0
					&& ve[i + 1].y != 0) {

				ed[i].x1 = ve[i].x;
				ed[i].x2 = ve[i + 1].x;
				ed[i].y1 = ve[i].y;
				ed[i].y2 = ve[i + 1].y;
			}
		}

		ed[Analysis_.numVertex_User_Defined - 1].x1 = ve[Analysis_.numVertex_User_Defined - 1].x;
		ed[Analysis_.numVertex_User_Defined - 1].y1 = ve[Analysis_.numVertex_User_Defined - 1].y;
		ed[Analysis_.numVertex_User_Defined - 1].x2 = ve[0].x;
    	ed[Analysis_.numVertex_User_Defined - 1].y2 = ve[0].y;
		
   // 	for (int i = 0; i < Analysis_.numVertex_User_Defined; i++) {

	//		System.out.println("xmass :"+center_mass.xMass+ "ymmass: "+center_mass.yMass +" x:" +ve[i].x +" y: "+ve[i].y);
			
		//	(Analysis_.imp_rgb_modified).getProcessor().drawLine(center_mass.xMass, center_mass.yMass, ve[i].x,  ve[i].y);
    
		//}

    	//Analysis_.imp_rgb_modified.updateAndDraw();
    	
    	
    	
		
		
		
	}
	
	

}